home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / basic / bastips2.zip / COLORS.TXT < prev    next >
Text File  |  1986-07-02  |  11KB  |  237 lines

  1.                 Million-Color Palette for IBM PC
  2.     (COMPUTE! Magazine December 1985 by John and Jeff Klein)
  3.  
  4.      No longer is your PC restricted to a palette of 16 colors and an
  5. inability to display them all in higher resolutions.  The secret is
  6. a combination of a techniques called tile painting and the trick of
  7. fooling a TV or composite monitor into displaying new solid colors.
  8.  
  9.      Graphics images are stored differently in the computer's memory
  10. for each different graphics mode or screen.  The color of each pixel
  11. is stored in a section of memory.  This video memory is arranged by
  12. its location or coordinates on the screen.  The image you see on the
  13. screen, therefore, is a copy of the contents of video memory.  To
  14. figure out how many pixels can be represented in a byte of memory,
  15. remember that a byte is made up of 8 bits.  Simply divide the amount
  16. of memory required for a certain screen mode by the number of pixels
  17. on the screen.  The memory requirements for each screen mode are:
  18.  
  19. Screen                   # of      Memory
  20.  Mode     Resolution    Colors   per Screen   Pixels/Byte   Bits/Pixel
  21.  
  22.   1       320 x 200       4         16K            4            2
  23.   2       620 x 200       2         16K            8            1
  24.  
  25.      Remember that RGB stands for the three primary colors of light:
  26. red, green and blue.  All colors can be made by mixing these three
  27. primary colors.  That's why RGB monitors, color TVs and composite color
  28. monitors have three electron guns, all of which are controlled by the
  29. computer to produce color.  If none of the guns is lighting a pixel,
  30. the pixel appears black.
  31.  
  32.      Colors are represented in memory by arranging bits to denote
  33. which electron guns should be turned on or off when lighting the
  34. corresponding pixel.  For instance, if a certain pixel is supposed to
  35. be blue, the group of bits representing that pixel in memory shows the
  36. blue gun is on and the others off.  All the possible combinations of
  37. the three electron guns account for eight colors.  To get eight more
  38. colors, the intensity, also called luminance, is varied by mixing a
  39. little white with the first eight colors.  That's why the IBM PC has
  40. a total of 16 color variations -- two shades each of eight colors.
  41. The 16 colors are represented by the following bit combinations.
  42.  
  43.  
  44.                     Bits
  45. Luminance     Red  Green  Blue     Color
  46.     0          0     0     0       Black
  47.     0          0     0     1       Blue
  48.     0          0     1     0       Green
  49.     0          0     1     1       Cyan
  50.     0          1     0     0       Red
  51.     0          1     0     1       Magenta
  52.     0          1     1     0       Brown
  53.     0          1     1     1       Light Gray
  54.     1          0     0     0       Dark Gray
  55.     1          0     0     1       Light Blue
  56.     1          0     1     0       Light Green
  57.     1          0     1     1       Light Cyan
  58.     1          1     0     0       Pink
  59.     1          1     0     1       Light Magenta
  60.     1          1     1     0       Yellow
  61.     1          1     1     1       White
  62.  
  63. Remember that each bit turns an electron gun either on or off.  Notice
  64. how many bits its takes to represent all the possible combinations.
  65. It takes four bits, or half a byte (sometimes called nybble) to
  66. represent all 16 colors.  So all screen modes which use four bits to
  67. represent a pixel are 16-color modes.  Only four-color combinations
  68. are possible with two bits, and only two combinations are possible
  69. with one bit.  That's why some screen modes can display only four or
  70. two colors at a time.
  71.  
  72.      Once you're familiar with how pixels are represented in video
  73. memory, the technique of tile painting is easy to understand.  Tile
  74. painting uses the PAINT command to fill the bytes of screen memory
  75. with certain patterns of ones and zeros.  The pattern is programmable,
  76. and it represents what is displayed on the TV or monitor.  Instead of
  77. painting with the actual color, you paint with the bit pattern of the
  78. color.  By using bit patterns, you can actually paint with more than
  79. one color around some specified border color:
  80.  
  81. PAINT (x,y),CHR$(bit pattern) + CHR$(bit pattern) + ...,boundary color
  82.  
  83.      The bit pattern consists of eight bits, so its decimal equivalent
  84. can range from 0 to 255 (integers only).  The bit pattern must
  85. represent the colors of the pixels per byte of the screen mode you're
  86. using.  The color patterns are put in memory next to each other as
  87. vertical lines on the screen.  This example paints SCREEN 1 with
  88. vertical bands of blue and green lines:
  89.  
  90. 10 SCREEN 1:CLS
  91. 20 PAINT (1,1),CHR$(102),3
  92.  
  93. The reason why the lines are blue and green can be seen when the
  94. number 102 is expressed in binary, revealing the bit pattern:
  95.  
  96. 102 = 01100110
  97.  
  98. Decimal 102 is derived from this binary number by:
  99.  
  100. Value for
  101. each digit   128   64   32   16   8   4   2   1   01 = 0001 = blue
  102.                                                   10 = 0010 = green
  103. Binary         0    1    1    0   0   1   1   0
  104.  
  105. 128 * 0 =  0
  106.  64 * 1 = 64
  107.  32 * 1 = 32
  108.  16 * 0 =  0
  109.   8 * 0 =  0
  110.   4 * 1 =  4
  111.   2 * 1 =  2
  112.   1 * 0 =  0
  113.          102
  114.  
  115.  
  116. SCREEN 1 stores four pixels per byte, so the pattern works out to:
  117.  
  118. 01       10        01       10
  119. blue     green     blue     green
  120.  
  121.      Here's where things get tricky.  If the computer is plugged into
  122. a color TV or composite color monitor (not an RGB monitor), you won't
  123. see the blue and green vertical lines that are supposed to be there.
  124. Instead, you'll see a solid bar of color that's sort of blue.  And the
  125. blue is not one of the normal 16 colors available.  It is a new color
  126. -- one of the 16 shades that can be created on SCREEN 1 of the PC.
  127.  
  128.      What's happening is something called artifacting.  This effect
  129. takes advantage of the limited resolution of TVs and composite color
  130. monitors.  When two very small pixels are placed next to each other
  131. on these screens, there isn't enough resolution to display them
  132. properly.  As a result, the pixels tend to blend together and create
  133. a false color -- an artifact color.  The color wouldn't be visible if
  134. the screen had more resolution.  RGB monitors have enough resolution
  135. to display the pixels as they're supposed to appear.
  136.  
  137.      If the binary pattern 10 01 10 01 is used in the above example
  138. instead of 01 10 01 10, the ahsde is slightly different -- blue-green-
  139. blue-green does not appear the same as green-blue-green-blue on a
  140. color TV or composite monitor.  They mix differently to create an
  141. entirely new shade of blue-green.
  142.  
  143.      The PC has only two graphics modes, SCREEN 1 and SCREEN 2.  Tile
  144. painting produces only 16 colors in SCREEN 1 and five shades of gray
  145. in SCREEN 2.  In SCREEN 1, tile painting lets us display up to 16 hues
  146. simultaneously.  Program 1 displays 16 shades.  Vertical bands with
  147. four colors don't blend in this mode, so somehow bands of two must be
  148. painted.  The secret is in line 40.  Since there are four pixels per
  149. byte, the last half of the byte has to be reflected in the first half.
  150. This technique insures that only two colors are in each band of four.
  151. The first half is the same as the last half, so the first band of two
  152. will be the same as the last band of two.
  153.  
  154.      Tile painting doesn't work correctly in SCREEN 2, high resolution
  155. with two colors, because this screen is always in black and white.
  156. However, you can get five shades of gray, as shown in Program 2.
  157. Solid lines form the brightest white.  Lines separated by two or three
  158. lines of black yield the next two shades.  The middle gray can't be
  159. displayed when using the PAINT command, because it's not possible to
  160. create a bit pattern that represents two blacks and then a white.
  161. These bit patterns generate the various shades of gray:
  162.  
  163.  
  164.  
  165.  
  166.                 Binary        Decimal   Hex      Shade
  167. color 1  =  1 1 1 1 1 1 1 1  =  256  =  &HFF  =  White
  168.             0 1 0 1 0 1 0 1  =   85  =  &H55  =  Dull White
  169.             (Not accessible)                  =  Middle Gray
  170.             0 0 0 1 0 0 0 1  =   17  =  &H11  =  Dark Gray
  171. color 0  =  0 0 0 0 0 0 0 0  =    0  =  &H00  =  Black
  172.  
  173.      Program 3 provides a demo of tile painting in SCREEN 1.  It fills
  174. the screen with circles, displaying up to 16 colors on the PC.
  175.  
  176.      Program 4, for the PC with an RGB monitor, demonstrates the
  177. usefulness of the many new colors in a fascinating experiment.  It
  178. uses SCREEN 1 and tile painting, but in a different way than Program 3.
  179. Closely spaced vertical lines don't blend together on an RGB monitor,
  180. so the previous technique won't work.  Program 4 uses the second part
  181. of the PAINT command.  The first CHR$(bit pattern) controls the
  182. horizontal line above the second CHR$(bit pattern).  Now the PAINT
  183. command can control the horizontal as well as the vertical lines,
  184. forming a checkerboard.  Although the checkerboard blends the lines
  185. together to create new colors, the colors aren't as solid as those
  186. produced by vertical lines on a TV or composite monitor.  Indeed, the
  187. effect won't look very pretty on a TV or composite monitor; it's
  188. passable on an RGB.
  189.  
  190.      To use the new colors in your own programs, choose one of the
  191. example programs.  This table summarizes the programs and the number
  192. of color variations possible in each.
  193.  
  194.            Screen     Max     Colors per    Display
  195. Program     Mode     Colors     Screen       Device
  196.  
  197.    1       SCREEN 1    16         16        TV or CC
  198.    2       SCREEN 2     5          5        TV or CC
  199.    3       SCREEN 1    16         16        TV or CC
  200.    4       SCREEN 1    20         10          RGB
  201.  
  202. Program 1:
  203. 10 SCREEN 1:CLS:KEY OFF:COLOR ,0
  204. 20 RANDOMIZE VAL(RIGHT$(TIME$,2)):Z=-1:A=INT(320/16):Y=0
  205. 30 FOR X=0 TO 15:Z=Z+1
  206. 40 LINE (X*A,0)-(X*A+A,200),3,B
  207. 50 IF Z<>0 THEN PAINT (X*A+1,1),CHR$(Z+Z*16),3
  208. 60 LINE (X*A,0)-(X*A+A,200),0,B
  209. 70 NEXT X
  210.  
  211. Program 2:
  212. 10 SCREEN 2,1:CLS:KEY OFF
  213. 20 FOR X=1 TO 100:LINE (X,1)-(X,200),1:NEXT X
  214. 30 FOR X=101 TO 200 STEP 2:LINE (X,1)-(X,200),1:NEXT X
  215. 40 FOR X=201 TO 300 STEP 3:LINE (X,1)-(X,200),1:NEXT X
  216. 50 FOR X=301 TO 400 STEP 4:LINE (X,1)-(X,200),1:NEXT X
  217. 60 GOTO 60
  218.  
  219.  
  220.  
  221. Program 3:
  222. 10 SCREEN 1:CLS:KEY OFF:COLOR ,0
  223. 20 RANDOMIZE VAL(RIGHT$(TIME$,2))
  224. 30 X=RND*320:Y=RND*200:R=RND*10+10:TILE=INT(RND*(15)+1)
  225. 40 CIRCLE (X,Y),R,3:PAINT (X,Y),CHR$(TILE+TILE*16),3:CIRCLE (X,Y),R,0
  226. 50 GOTO 20
  227.  
  228. Program 4:
  229. 10 SCREEN 1:CLS:KEY OFF:COLOR ,0
  230. 20 RANDOMIZE VAL(RIGHT$(TIME$,2)):Z=-1:A=INT(320/16):Y=0:C=0
  231. 30 FOR X=0 TO 15:Z=Z+1
  232. 40 LINE (X*A,0)-(X*A+A,200),3,B:Y=Z+Z*16:Q=Y*4:R=INT(Q/256):Q=Q-R*256+R
  233. 50 IF Z<>0 THEN PAINT (X*A+1,1),CHR$(Y)+CHR$(Q),3
  234. 60 LINE (X*A,0)-(X*A+A,200),0,B
  235. 70 NEXT X
  236.  
  237.